package com.i2863.receipt.dao;

import com.i2863.receipt.entity.Prize;
import com.i2863.receipt.entity.Winner;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;


/**
 * Created by Administrator on 2018/6/8.
 */
public interface PrizeRepository extends JpaRepository<Prize,Integer>,JpaSpecificationExecutor<Prize>{


    /**
     * 根据站点ID和奖品等级 统计奖项的数量
     * @param level
     * @param siteId
     * @return
     */
    @Query(value = "select count(*) from Prize where level=:level and site.id=:siteId")
    Integer countLevelPrizeBySiteId(@Param(value = "level")Integer level, @Param(value = "siteId")Integer siteId);


    @Query(value = "select sum(number) from d_prize where site_id = :siteId and level = :level and prize_range = :prizeRange",nativeQuery = true)
    Integer sumBySiteIdAndLevelAndPrizeRange(@Param("siteId")Integer siteId,@Param("level")Integer level,@Param("prizeRange")Integer  prizeRange);

    /**
     * 查询具体级别的奖项内容
     * @param siteId 站点id
     * @param level 奖项级别
     * @param prizeRange 范围，场内还是复活
     * @return  奖项内容
     */
    List<Prize> findBySiteIdAndLevelAndPrizeRange(@Param("siteId")Integer siteId,@Param("level")Integer level,@Param("prizeRange")Integer  prizeRange);


    /**
     * 查询指定等级的所有奖项
     * @param siteId  站点id
     * @param prizeRange 范围,场内还是复活
     * @param level 奖项等级
     * @return  站点下的奖项内容
     */
    List<Prize> findBySiteIdAndPrizeRangeAndLevel(Integer siteId,Integer  prizeRange,Integer level);



    /**
     * 查询活动的所有奖项
     * @param siteId  站点id
     * @param prizeRange 范围,场内还是复活
     * @return  站点下的奖项内容
     */
    List<Prize> findBySiteIdAndPrizeRange(Integer siteId,Integer  prizeRange);


    /**
     * 按奖品等级分组 按站点查询
     * @param siteId
     * @return
     */
    @Query(value = "select * from d_prize where site_id=:siteId group by level",nativeQuery = true)
    List<Prize> groupByLevel(@Param("siteId")Integer siteId);

}
